package com.googlecode.linkmein.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Projects extends ArrayList<Project> {
    public static Projects get(Employees employees) {
        Projects projects = new Projects();
        for (Employee employee : employees) {
            Project project = employee.getProject();
            if (!projects.contains(project)) {
                projects.add(project);
            }
            Project p = projects.get(projects.indexOf(project));
            p.addMember(employee);
            employee.setProject(p);
        }
        return projects;
    }

    public Map<Project, Employees> orderedByProjects() {
        Map<Project, Employees> result = new HashMap<Project, Employees>();
        for (Project project : this) {
            if (result.containsKey(project)) {
                Employees teamates = result.get(project);
                teamates.addAll(project.getMembers());
            } else {
                Employees team = new Employees();
                team.addAll(project.getMembers());
                result.put(project, team);
            }
        }
        return result;
    }
}
